const app = getApp()
Component({
  properties: {
    fruitsData: {
      type: Object,
      value: null
    },
    shopCarData: {
      type: Object,
      value: null
    }
  },
  data: {
    static: true,
    list: []
  },
  lifetimes: {
    attached() {
      this.setData({
        list: this.properties.shopCarData
      })
    }
  },
  methods: {
    //添加
    addCar() {
      let newShop = app.globalData.shopCar.filter(e => e.name == this.properties.fruitsData.name)[0]
      if (newShop != null && newShop != undefined) {
        newShop.numb += 1
      } else {
        app.globalData.shopCar.push({
          name: this.properties.fruitsData.name,
          price: this.properties.fruitsData.price,
          numb: 1
        })
      }
      wx.showToast({
        title: '添加成功',
      })
      console.log(app.globalData.shopCar)
    },
    // 减
    jian() {
      const {
        name,
        price
      } = this.data.list
      let numb = this.data.list.numb - 1
      this.setData({
        list: {
          name,
          price,
          numb
        }
      })
      let i = app.globalData.shopCar.findIndex(e => e.name === this.data.list.name)
      if (i > -1) {
        if (numb <= 0) {
          app.globalData.shopCar.splice(i, 1)
        } else {
          app.globalData.shopCar[i].numb = numb
        }
      }
      this.triggerEvent("gaibian")
    },
    //加
    jia() {
      const {
        name,
        price,
        numb
      } = this.data.list
      this.setData({
        list: {
          name,
          price,
          numb: numb + 1
        }
      })
      let i = app.globalData.shopCar.findIndex(e => e.name == this.data.list.name)
      app.globalData.shopCar[i].numb = this.data.list.numb
      this.triggerEvent("gaibian")
    },
    //是否选中
    changeStatic() {
      this.setData({
        static: !this.data.static
      })
      let sum = this.data.list.numb * this.data.list.price
      let s = this.data.static
      let data = {
        sum,
        s
      }
      this.triggerEvent("quexiao", data)
    }
  },
})